//
//  CLCaseDisplayController.m
//  CareLink
//
//  Created by tanky on 16/8/20.
//  Copyright © 2016年 Seven Team. All rights reserved.
//

#import "CLCaseDisplayController.h"
#import "CLEditCaseTableViewController.h"

@interface CLCaseDisplayController ()

@property(nonatomic,assign)float CellHeight;
@property(nonatomic,assign)float imageDisplayCellHeight;

@end

@implementation CLCaseDisplayController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupUI];
}

- (void)setupUI{
    self.tableView.bounces = NO;
    self.tableView.tableFooterView = [UIView new];
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(.01 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        NSIndexPath *indexPath = [NSIndexPath indexPathForRow:1 inSection:0];
        UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
        float cellRowHeight = cell.detailTextLabel.bounds.size.height + cell.detailTextLabel.frame.origin.y + 20;
        self.CellHeight = cellRowHeight;
        [self.tableView reloadData];
    });
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"编辑" style:UIBarButtonItemStylePlain target:self action:@selector(editing)];
}

- (void)editing{
    CLEditCaseTableViewController *editCaseViewController = [CLEditCaseTableViewController new];
    editCaseViewController.caseDisplayDict = self.caseDisplayDict;
    [self showViewController:editCaseViewController sender:nil];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 4;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row == 0) {
        UITableViewCell *cell = [self loadDefaultCellWithTitleTitle:@"疾病类型" andContentString:self.caseDisplayDict[@"diseaseDetail"]];
        return cell;
    }else if (indexPath.row == 1){
        UITableViewCell *cell = [self loadDefaultCellWithTitleTitle:@"疾病描述" andContentString:self.caseDisplayDict[@"symptomDes"]];
        return cell;
    }else if (indexPath.row == 2){
        UITableViewCell *cell = [self loadDefaultCellWithTitleTitle:@"疾病图片:" andContentString:@""];
        return cell;
    }
    UITableViewCell *cell = [self loadImageDisplayCellWithImageNames:nil];
    return cell;
}

- (UITableViewCell *)loadDefaultCellWithTitleTitle:(NSString *)titleString andContentString:(NSString *)contentString{
    UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"caseDisplayDefault_cellid"];
    cell.textLabel.text = titleString;
    cell.detailTextLabel.text = contentString;
    cell.detailTextLabel.numberOfLines = 0;
    cell.detailTextLabel.lineBreakMode = NSLineBreakByWordWrapping;
    return cell;
}

- (UITableViewCell *)loadImageDisplayCellWithImageNames:(NSArray *)imageNames{
    UITableViewCell *cell = [[UITableViewCell alloc] init];
    float margin = 10;
    float buttonWidth = self.tableView.bounds.size.width / 3 - margin * 4 / 3;
    int picCount = 1;
    for (int i = 0; i < picCount; i ++) {
        UIButton *btn = [[UIButton alloc] init];
        [btn setImage:self.caseDisplayDict[@"image"] forState:UIControlStateNormal];
        btn.frame = CGRectMake(margin + buttonWidth * (i % 3 ) + margin * (i % 3), margin + buttonWidth * (i / 3) + margin * (i / 3), buttonWidth, buttonWidth);
        [cell addSubview:btn];
    }
    self.imageDisplayCellHeight = ( margin * 1.5 + buttonWidth ) * (6 / 3);
    return cell;
}



- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row == 1) {
        if (self.CellHeight) {
            return self.CellHeight;
        }else{
            return [super tableView:tableView heightForRowAtIndexPath:indexPath];
        }
    } else if (indexPath.row == 3){
        return self.imageDisplayCellHeight;
    }
    return [super tableView:tableView heightForRowAtIndexPath:indexPath];
}


/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
